<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';

      run(async function () {
        const clock = lolex.createClock();
        const directLine = testHelpers.createDirectLineWithTranscript([], { ponyfill: clock });

        WebChat.renderWebChat(
          {
            directLine,
            ponyfill: clock,
            store: testHelpers.createStoreWithOptions({ ponyfill: clock })
          },
          document.getElementById('webchat')
        );

        await pageConditions.webChatRendered();

        clock.tick(600);

        await pageConditions.uiConnected();

        const liveRegionElement = pageElements.liveRegion();
        const liveRegionInnerTexts = [];

        const mutationObserver = new MutationObserver(records => {
          liveRegionInnerTexts.push(
            ...[].reduce.call(
              records,
              (addedInnerTexts, record) =>
                [].reduce.call(
                  record.addedNodes,
                  (addedInnerTexts, { innerText }) => [...addedInnerTexts, innerText],
                  addedInnerTexts
                ),
              []
            )
          );
        });

        mutationObserver.observe(liveRegionElement, { childList: true });

        // WHEN: At t = 0, a typing activity is sent by the bot.
        directLine.activityDeferredObservable.next({
          channelId: 'directline',
          from: {
            id: 'bot-id',
            name: 'bot-id',
            role: 'bot'
          },
          id: 'a00001',
          timestamp: new Date().toISOString(),
          type: 'typing'
        });

        // THEN: It should read "Bot is typing."
        await waitFor(() => expect(liveRegionInnerTexts.join('\n')).toBe('Bot is typing.'));

        // THEN: The typing indicator should be shown.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t = 1, another typing activity is sent by the bot.
        clock.tick(1000);
        directLine.activityDeferredObservable.next({
          channelId: 'directline',
          from: {
            id: 'bot-id',
            name: 'bot-id',
            role: 'bot'
          },
          id: 'a00002',
          timestamp: new Date().toISOString(),
          type: 'typing'
        });

        // THEN: It should not add any live region elements.
        await waitFor(() => expect(liveRegionInnerTexts.join('\n')).toBe('Bot is typing.'), 5_000);

        // THEN: The typing indicator should be shown.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t = 6.
        clock.tick(5000);

        // THEN: It should not add any live region elements.
        await waitFor(() => expect(liveRegionInnerTexts.join('\n')).toBe('Bot is typing.'), 5_000);

        // THEN: The typing indicator should be hidden.
        await pageConditions.typingIndicatorHidden();
      });
    </script>
  </body>
</html>
